package aad.storyboard.view;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;
import aad.storyboard.controller.StoryTimer;
import aad.storyboard.model.Scene;
import aad.storyboard.model.Script;
import aad.storyboard.model.Story;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.webkit.WebSettings;
import android.webkit.WebView;

/**
 * Can be used as an alternative to the StaticScriptView if the subtitles should contain clickable words that will trigger the JavaScriptInterface and display the WikiView 
 */
public class ClickableScriptView extends WebView implements Observer{
	private static Story story;
	private JavaScriptInterface jInterFace;
		
	private ArrayList <Script> scripts;
	private String subtitle;
	private double subtime=0;
	private double subend=0;
	private Integer inter=0;
	private int currentTime=0;
	private String js_data;

	public ClickableScriptView(Context context,Story s) {
		super(context);		
		
		WebSettings webSettings = getSettings();
	    webSettings.setJavaScriptEnabled(true);
        setBackgroundColor(Color.TRANSPARENT);  	  
        loadData("", "text/html", "utf-8");  
	}	
	
	public ClickableScriptView(Context context) {
		super(context);		
		
		WebSettings webSettings = getSettings();
	    webSettings.setJavaScriptEnabled(true);
        setBackgroundColor(Color.TRANSPARENT);  	  
        loadData("", "text/html", "utf-8");  
	}	
	
	public ClickableScriptView(Context c, AttributeSet attrs) {
			super(c, attrs);
		WebSettings webSettings = getSettings();
	    webSettings.setJavaScriptEnabled(true);
        setBackgroundColor(Color.TRANSPARENT);  	  
        loadData("", "text/html", "utf-8");  
	}	
	
	public void setJavaScriptInterface(JavaScriptInterface itf)
	{
		addJavascriptInterface(itf, "Android");
	}
	
	@Override
	public void onDraw(Canvas canvas){		
		super.onDraw(canvas);
		
		loadData(subtitle + js_data+currentTime, "text/html", "utf-8");
        addJavascriptInterface(jInterFace, "Android");		
		invalidate();
	}

	@Override
	public void update(Observable timer, Object arg1) {
		
		currentTime=((StoryTimer)timer).getCurrentTime();
				
		Scene currentScene = Story.getInstance().getCurrentChapter().getCurrentScene();
		
		for(int i=0; i<currentScene.getScripts().size(); i++){			
	          if(currentTime>=currentScene.getScripts().get(i).getStartTime()&&!currentScene.getScripts().get(i).isStarted()&&currentTime<=currentScene.getScripts().get(i).getEndTime()){	       	   
	        	  currentScene.setCurrentScript(currentScene.getScripts().get(i));
	        	  currentScene.getScripts().get(i).setStarted(true);
	        	  subtitle=currentScene.getScripts().get(i).getText();
	        	  String word=currentScene.getScripts().get(i).getClickableWord();
	        	  js_data = "<script type=\"text/javascript\">function showWikiPage("+word+"){Android.showExplanation("+word+");}</script>";
	        	  subtime=currentScene.getScripts().get(i).getStartTime();
	        	  subend=currentScene.getScripts().get(i).getEndTime();	        	  
	        	  break;
	          }
	          
		 }		
          
	}	
	

}
